#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int T;
  cin >> T;
  while (T --) {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i ++) cin >> a[i];
    a.insert(a.end(), a.begin() + 1, a.end());
    n *= 2;
    vector<vector<i64>> dp(n + 2, vector<i64>(n + 2));
    for (int l = n; l >= 1; l --) {
      for (int r = l; r <= n; r ++) {
        dp[l][r] = max(dp[l][r - 1], dp[l + 1][r]);
        for (int k = l + 1; k < r; k ++) {
          dp[l][r] = max(dp[l][r], dp[l][k] + dp[k + 1][r]);
          dp[l][r] = max(dp[l][r], dp[l+1][k-1] + dp[k+1][r-1] + a[l] * a[k] * a[r]);
        }
      }
    }
    i64 ans = 0;
    n /= 2;
    for (int i = 1; i <= n; i ++) {
      ans = max(ans, dp[i][i + n - 1]);
    }
    cout << ans << endl;
  }
}